/**
 * 
 */
package be.bejug.scrumadmin.domain;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * @author Glenn Dejaeger
 * 
 */
@Entity
@Table(name = "SPRINTS", schema = "SCRUMADMIN")
public class Sprint extends DomainObject implements Serializable,
		Comparable<Sprint> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8198938146170524782L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	@Column(name = "DATEFROM", nullable = false)
	private Date from;

	@Column(name = "DATETO", nullable = false)
	private Date to;

	@OneToMany(mappedBy = "sprint")
	private Set<Story> stories = new HashSet<Story>();

	/**
	 * @param id
	 * @param from
	 * @param to
	 */
	public Sprint(final int id, final Date from, final Date to) {
		super();
		this.id = id;
		this.from = from;
		this.to = to;
	}

	/**
	 * @return the id
	 */
	@Override
	public Integer getId() {
		return id;
	}

	public Date getFrom() {
		return from;
	}

	public void setFrom(final Date from) {
		this.from = from;
	}

	public Date getTo() {
		return to;
	}

	public void setTo(final Date to) {
		this.to = to;
	}

	public Set<Story> getStories() {
		return stories;
	}

	public void addStories(final Story... storiesToAdd) {
		for (Story story : storiesToAdd) {
			// story.setSprint(this);
			stories.add(story);
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (prime * result) + ((from == null) ? 0 : from.hashCode());
		result = (prime * result) + ((to == null) ? 0 : to.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Sprint other = (Sprint) obj;
		if (from == null) {
			if (other.from != null) {
				return false;
			}
		} else if (!from.equals(other.from)) {
			return false;
		}
		if (to == null) {
			if (other.to != null) {
				return false;
			}
		} else if (!to.equals(other.to)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		return sdf.format(from) + " - " + sdf.format(to);
	}

	@Override
	public int compareTo(final Sprint sprint) {
		return this.getFrom().compareTo(sprint.getFrom());
	}
}
